
package asginamentIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AsignamentIOClass {
    public static final String fileName = "numbers.txt";
    private static final String fileWrited = "sort.txt";
    
    public void toPrintArrayComplete() {
           List<Integer> arrayListInteger = new ArrayList<Integer>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));
            
            String line = null;
            try {
                while((line = bufferedReader.readLine()) != null) {  
                    System.out.println(""+line.toString());
                    arrayListInteger.add(toConvertInteger(line));                    
                }
            } catch (IOException ex) {
                Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public List<Integer> toReadFileComplet(String str) {
        List<Integer> arrayListInteger = new ArrayList<Integer>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));
            
            String line = null;
            try {
                while((line = bufferedReader.readLine()) != null) {                    
                    arrayListInteger.add(toConvertInteger(line));                    
                }
            } catch (IOException ex) {
                Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
        }        
        
        return arrayListInteger;
    }
    
    public Integer toConvertInteger(String line) {              
        try {
            return Integer.parseInt(line);
        } catch (Exception e) {
            return 0;
        }
    }
           
    public void getMajorLess(List<Integer> arrayCompleteIntegerToSearchLessNumber){                    
        System.out.println("El numero menor es el :"+Collections.min(arrayCompleteIntegerToSearchLessNumber));
        System.out.println("EL numero mayor es el :"+Collections.max(arrayCompleteIntegerToSearchLessNumber));
    }
    
    public void arraySortedSelections(List<Integer> sortedArrayListInteger) {                                
        FileWriter fileWriter = null;
        try {
            Comparator<Integer> ll = Collections.reverseOrder();
            Collections.sort(sortedArrayListInteger, ll);
            System.out.println(sortedArrayListInteger);
            
            char[] in = new char[sortedArrayListInteger.size()];
            
            File file = new File("sort.txt");
            fileWriter = new FileWriter(file);
            fileWriter.write(sortedArrayListInteger.toString()+"\n");            
            fileWriter.flush();
            fileWriter.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(AsignamentIOClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
           
        
    }
        public static void main(String[] args) {
        
            AsignamentIOClass asignacionToWorkReadFile = new AsignamentIOClass();                                               
                                    
            asignacionToWorkReadFile.getMajorLess(asignacionToWorkReadFile.toReadFileComplet(fileName));
                        
            asignacionToWorkReadFile.arraySortedSelections(asignacionToWorkReadFile.toReadFileComplet(fileName));
            
            
    }

    
}
